package com.lxl.view;

import java.awt.EventQueue;
import java.awt.ScrollPane;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.lxl.model.BookClassify;
import com.lxl.service.BookClassifyService;
import com.lxl.service.impl.BookClassifyServiceImpl;

import javax.swing.JLabel;


import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Cursor;

/**
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月12日 上午11:43:05
 */

public class ViewBookClassify extends JFrame {

	static BookClassifyService bookClassifyService = new BookClassifyServiceImpl();
	static String[][] bookClassifyList = new String[100][1];
	static List<BookClassify> list = bookClassifyService.list();
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	private JLabel lblNewLabel;
	private JButton button_1;
	private JButton button_2;
	private JButton button_3;
	
	public static void main(String[] args) {

		for (int j = 0; j < list.size(); j++)
			bookClassifyList[j][0] = list.get(j).getBookClass();

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ViewBookClassify frame = new ViewBookClassify();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public ViewBookClassify() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				ManagerView.main(new String[1]);
			}
		});
		setFont(new Font("微软雅黑", Font.PLAIN, 12));
		setTitle("图书管理系统");
		setBounds(100, 100, 450, 278);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblNewLabel = new JLabel("类型名");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel.setBounds(187, 0, 65, 15);
		contentPane.add(lblNewLabel);
		
		ScrollPane scrollPane = new ScrollPane();
		scrollPane.setBounds(0, 24, 434, 175);
		contentPane.add(scrollPane);

		table = new JTable();
		table.setEnabled(false);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		table.setBounds(5, 20, 424, 175);
		table.setModel(new DefaultTableModel(bookClassifyList,
				new String[]{"\u7528\u6237\u7F16\u53F7"}));
		scrollPane.add(table);

		JButton button = new JButton("删除类型");
		button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				DeleteBookClassify.main(new String[1]);
				dispose();
			}
		});
		button.setBounds(118, 205, 93, 23);
		contentPane.add(button);

		button_1 = new JButton("修改类型");
		button_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				MergeBookClassify.main(new String[1]);
				dispose();
			}
		});
		button_1.setBounds(221, 205, 93, 23);
		contentPane.add(button_1);

		button_2 = new JButton("添加类型");
		button_2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				AddBookClassify.main(new String[1]);
				dispose();
			}
		});
		button_2.setBounds(15, 205, 93, 23);
		contentPane.add(button_2);
		
		button_3 = new JButton("刷新");
		button_3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				list = bookClassifyService.list();
				for (int j = 0; j < list.size(); j++)
					bookClassifyList[j][0] = list.get(j).getBookClass();	
				table.setModel(new DefaultTableModel(bookClassifyList,
						new String[]{"\u7528\u6237\u7F16\u53F7"}));
				JOptionPane.showMessageDialog(null, "刷新成功！");
			}
		});
		button_3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_3.setBounds(324, 205, 93, 23);
		contentPane.add(button_3);
	}
}
